package ui.swing.table;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class Table_AbstractTbmImpl2 extends JFrame implements ActionListener{

	private static final long serialVersionUID = -3881505100386065633L;
	
	JTable tblGrade = null;
	
	public Table_AbstractTbmImpl2() {
		super("自己写的，可以切换model");
		setBounds(180, 180, 300, 300);
		
		JPanel pnlTop = new JPanel();
		JButton btnTch = new JButton("数学老师");
		btnTch.addActionListener(this);
		JButton btnStu = new JButton("学生阿呆");
		btnStu.addActionListener(this);
		pnlTop.add(btnTch);
		pnlTop.add(btnStu);
		
		JPanel pnlTblContainer = new JPanel(new BorderLayout());
		tblGrade = new JTable(MyTableModel.getInstance("student"));
		pnlTblContainer.add(tblGrade.getTableHeader(), BorderLayout.NORTH);
		pnlTblContainer.add(tblGrade, BorderLayout.CENTER);
		
		getContentPane().add(pnlTop, BorderLayout.NORTH);
		getContentPane().add(pnlTblContainer, BorderLayout.CENTER);
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
		
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("学生阿呆"))
			tblGrade.setModel(MyTableModel.getInstance("student"));
        if (e.getActionCommand().equals("数学老师"))
        	tblGrade.setModel(MyTableModel.getInstance("teacher"));
        tblGrade.revalidate();
	}
	
	public static void main(String[] args) {
		new Table_AbstractTbmImpl2();
	}

	static class MyTableModel extends AbstractTableModel {

		private static final long serialVersionUID = -24231702853081600L;
		
		private static final HashMap<String, MyTableModel> instanceMap = new HashMap<String, MyTableModel>();
		
		static {
			MyTableModel model1 = new MyTableModel() {

				private static final long serialVersionUID = -2600286683480062511L;

				{
					p = new Object[][]{
							{"阿呆", new Integer(66), new Integer(32), new Integer(98), new Boolean(false), new Boolean(false)},
							{"阿瓜", new Integer(85), new Integer(69), new Integer(154), new Boolean(true),new Boolean(false)}
					};
					n = new String[]{"姓名","语文","数学","总分","及格","作弊"};
				}
			};
			instanceMap.put("student", model1);

			MyTableModel model2 = new MyTableModel() {

				private static final long serialVersionUID = 7809421631408820501L;

				{
					p = new Object[][]{
							{"阿呆", "1234",new Integer(50), new Boolean(false),new Boolean(false),"01234"},
					        {"阿瓜", "1235",new Integer(75), new Boolean(true),new Boolean(false),"05678"}
					};
					n = new String[]{"姓名","学号","数学","及格","作弊","电话"};
				}
			};
			instanceMap.put("teacher", model2);
		}
		
		public static MyTableModel getInstance(String key) {
			return instanceMap.get(key);
		}
		
		private MyTableModel(){}
		
		Object[][] p;
		String[] n;

		public int getColumnCount() {
			return n.length;
		}

		// getRowCount() is called frequently by JTable for display purposes
		// therefore, it should be designed with efficiency in mind.
		public int getRowCount() {
			return p.length;
		}

		public Object getValueAt(int rowIndex, int columnIndex) {
			return p[rowIndex][columnIndex];
		}
		
		// 重写这个方法定义每列的类型，用于分配默认的renderer和editor
		// used to assign a default renderer and editor for that column
		@Override
		public Class<?> getColumnClass(int c) {
			return getValueAt(0, c).getClass();
		}

		// 重写这个方法定义列名
		@Override
		public String getColumnName(int col) {
			return n[col];
		}

		// 重写这个方法定义单元格是否何以编辑
		@Override
		public boolean isCellEditable(int rowIndex, int columnIndex) {
			return true;
		}

		// 重写这个说明如何设置单元格数据，实现时必须根据isCellEditable的情况
		@Override
		public void setValueAt(Object value, int row, int col) {
			/*if(isCellEditable(row, col)) {
				p[row][col] = value;
				fireTableCellUpdated(row, col);
			}*/
			// 由于在本例子中肯定是返回true，所以可以直接这样：
			p[row][col] = value;
			fireTableCellUpdated(row, col);
		}
	}
}
